package com.maimang.platform.crm.service.impl;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.maimang.platform.crm.config.URLConstant;
import com.maimang.platform.crm.service.DingTalkUserService;
import com.maimang.platform.crm.util.AccessTokenUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname DingTalkRoleServiceImpl
 * @description <br>钉钉角色管理服务层实现</br>
 * @date 2019-09-22 14:18
 * @email jintaozhao@qq.com
 */
@Service("dingTalkUserService")
public class DingTalkUserServiceImpl implements DingTalkUserService {
	/**
	 * 获取部门用户
	 *
	 * @param deptId 部门id
	 * @param offset 分页偏移量
	 * @param size   分页大小
	 * @param order  支持分页查询，部门成员的排序规则，默认 是按自定义排序；
	 *               entry_asc：代表按照进入部门的时间升序，
	 *               entry_desc：代表按照进入部门的时间降序，
	 *               modify_asc：代表按照部门信息修改时间升序，
	 *               modify_desc：代表按照部门信息修改时间降序，
	 *               custom：代表用户定义(未定义时按照拼音)排序
	 * @return OapiUserSimplelistResponse
	 * @throws Exception
	 */
	@Override
	public OapiUserSimplelistResponse getSimpleList(Long deptId, Long offset, Long size, String order) throws Exception {
		String accessToken = AccessTokenUtil.getToken();
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.USER_SIMPLELIST);
		OapiUserSimplelistRequest request = new OapiUserSimplelistRequest();
		request.setDepartmentId(deptId);
		request.setOffset(offset);
		request.setSize(size);
		request.setHttpMethod("GET");
		OapiUserSimplelistResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取部门用户详情
	 *
	 * @param deptId 部门id
	 * @param offset 分页偏移量
	 * @param size   分页大小
	 * @param order  支持分页查询，部门成员的排序规则，默认 是按自定义排序；
	 *               entry_asc：代表按照进入部门的时间升序，
	 *               entry_desc：代表按照进入部门的时间降序，
	 *               modify_asc：代表按照部门信息修改时间升序，
	 *               modify_desc：代表按照部门信息修改时间降序，
	 *               custom：代表用户定义(未定义时按照拼音)排序
	 * @return OapiUserListbypageResponse
	 * @throws Exception
	 */
	@Override
	public OapiUserListbypageResponse listByPage(Long deptId, Long offset, Long size, String order) throws Exception {
		String accessToken = AccessTokenUtil.getToken();
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.USER_LIST_BY_PAGE);
		OapiUserListbypageRequest request = new OapiUserListbypageRequest();
		request.setDepartmentId(deptId);
		request.setOffset(offset);
		request.setSize(size);
		request.setOrder("entry_desc");
		request.setHttpMethod("GET");
		OapiUserListbypageResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取管理员列表
	 *
	 * @return 管理员列表
	 * @throws Exception
	 */
	@Override
	public OapiUserGetAdminResponse getAdminList() throws Exception {
		String accessToken = AccessTokenUtil.getToken();
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.GET_ADMIN);
		OapiUserGetAdminRequest request = new OapiUserGetAdminRequest();
		request.setHttpMethod("GET");
		OapiUserGetAdminResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取管理员通讯录权限范围
	 *
	 * @param userId 员工id
	 * @return OapiUserGetAdminScopeResponse
	 * @throws Exception
	 */
	@Override
	public OapiUserGetAdminScopeResponse getAdminScope(String userId) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.GET_ADMIN_SCOPE);
		String accessToken = AccessTokenUtil.getToken();
		OapiUserGetAdminScopeRequest request = new OapiUserGetAdminScopeRequest();
		request.setUserid(userId);
		OapiUserGetAdminScopeResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 根据unionid获取userid
	 *
	 * @param unionId 员工在当前开发者企业账号范围内的唯一标识，系统生成，固定值，不会改变
	 * @return OapiUserGetUseridByUnionidResponse
	 * @throws Exception
	 */
	@Override
	public OapiUserGetUseridByUnionidResponse getUseridByUnionid(String unionId) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient(URLConstant.USER_SIMPLELIST);
		String accessToken = AccessTokenUtil.getToken();
		OapiUserGetUseridByUnionidRequest request = new OapiUserGetUseridByUnionidRequest();
		request.setUnionid(unionId);
		request.setHttpMethod("GET");
		OapiUserGetUseridByUnionidResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取用户详情
	 *
	 * @param userid
	 * @return
	 * @throws Exception
	 */
	@Override
	public OapiUserGetResponse getUserByUserid(String userid) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
		OapiUserGetRequest request = new OapiUserGetRequest();
		request.setUserid(userid);
		request.setHttpMethod("GET");
		String accessToken = AccessTokenUtil.getToken();
		OapiUserGetResponse response = client.execute(request, accessToken);
		return response;
	}

	/**
	 * 获取部门用户userid列表
	 *
	 * @param deptId 部门id
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<String> getDeptMember(String deptId) throws Exception {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getDeptMember");
		OapiUserGetDeptMemberRequest req = new OapiUserGetDeptMemberRequest();
		req.setDeptId(deptId);
		req.setHttpMethod("GET");
		String accessToken = AccessTokenUtil.getToken();
		OapiUserGetDeptMemberResponse rsp = client.execute(req, accessToken);
		if (rsp.getErrcode() == 0L)
			return rsp.getUserIds();
		return new ArrayList<>();
	}
}
